package com.eduorder.controller;


import com.JwtUtils;
import com.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eduorder.entity.Order;
import com.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {

	@Autowired
	private OrderService orderService;

	//1 生成订单的方法
	@PostMapping("/createOrder/{courseId}")
	public R saveOrder(@PathVariable String courseId, HttpServletRequest request){

		//创建订单，返回订单号
		String orderNo = orderService.createOrder(courseId,JwtUtils.getMemberIdByJwtToken(request));
		return R.ok().data("orderId",orderNo);
	}

	@GetMapping("/getOrderInfo/{orderId}")
	public R getOrderInfo(@PathVariable String orderId){

		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("order_no",orderId);
		Order one = orderService.getOne(wrapper);

		return R.ok().data("item",one);
	}

	//根据课程id和用户id查询订单表中订单状态
	@GetMapping("/isBuyCourse/{courseId}/{memberId}")
	public boolean isBuyCourse(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId){

		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id",courseId);
		wrapper.eq("member_id",memberId);
		wrapper.eq("status",1);
		int count = orderService.count(wrapper);
		System.out.println("---------------------count:  "+count);

		if (count>0){ //已支付
			return true;
		}else {
			return false;
		}
	}
}

